//
//  HMSearchTableViewController.m
//  HowToDo
//
//  Created by qu on 2017/1/6.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMSearchTableViewController.h"
#import "HMSearchViewModel.h"
#import "HMSearchModel.h"
#import "SVProgressHUD.h"
#import "HMHomeRecommendDetailTVC.h"
@interface HMSearchTableViewController ()<UISearchBarDelegate>

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) UISearchBar *searchBar;

@end

@implementation HMSearchTableViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_searchBar becomeFirstResponder];
}
- (NSArray *)dataArray {
    
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"搜索";
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 414, 50)];
    _searchBar.delegate =self;
    _searchBar.placeholder = @"请输入要搜索的内容";
    self.tableView.tableHeaderView =_searchBar;
    
    _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    if (searchBar.text.length > 0) {
        
        [HMSearchViewModel loadDataWith:searchBar.text finishBlock:^(id result) {
            self.dataArray = result;
            [self.tableView reloadData];
            
        }];
    } else {
        
        [SVProgressHUD showInfoWithStatus:@"请输入要搜索内容"];
        [SVProgressHUD setMinimumDismissTimeInterval:0.3];
    }

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    HMSearchModel *model = self.dataArray[indexPath.row];
    
    cell.textLabel.text = model.name;

    return cell;
    
}
// 跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMSearchModel *model = self.dataArray[indexPath.row];
    
    HMHomeRecommendDetailTVC *detailTVC = [[HMHomeRecommendDetailTVC alloc]init];
    detailTVC.resource_id = model.tid.description;
    
   [self.navigationController pushViewController:detailTVC animated:YES];
   
    
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsCancelButton = YES;
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"搜索" forState:UIControlStateNormal];
            cancel.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
}



@end
